﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using wheel.Services;
using System.Text;

namespace wheel.Models
{
    public class GameInfo
    {
        public WordDetails WordToFind { get; set; }
        public string WordToFindObscured
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (char c in WordToFind.Definition)
                {
                    if (c == ' ')
                        sb.Append(' ');
                    else
                        sb.Append('#');
                }
                return sb.ToString();
            }
        }
        public List<UserChoice> UserChoices { get; set; }
        public bool WordDiscovered { get; set; }
        public bool HighScoreRegistered { get; set; }
        
        

        public GameInfo()
        {
            UserChoices = new List<UserChoice>();
            WordDiscovered = true;
            WordToFind = PearsonLongmanDictionaryService.GetRandomWord();
        }

        

        public decimal TotalScore
        {
            get
            {
                return UserChoices.Sum(uc => uc.Score);
            }
        }

        public static GameInfo Current
        {
            get
            {
                GameInfo info = HttpContext.Current.Session[typeof(GameInfo).ToString()] as GameInfo ;
                if (info == null)
                {
                    lock (typeof(GameInfo))
                    {
                        info = new GameInfo();
                        HttpContext.Current.Session[typeof(GameInfo).ToString()] = info;
                    }
                }
                return info;
            }
        }

        public static void Reset()
        {
            HttpContext.Current.Session[typeof(GameInfo).ToString()] = null;
        }
    }

    public class UserChoice
    {
        public char Letter{get; set;}
        public int Score { get; set; }
        public DateTime Time {get; set;}
    }
}